package com.huluobo.lc.kotlinim1807ak.ui.activity

import com.huluobo.lc.kotlinim1807ak.R
import com.huluobo.lc.kotlinim1807ak.base.BaseActivity
import com.huluobo.lc.kotlinim1807ak.contract.register.RegisterContract
import com.huluobo.lc.kotlinim1807ak.contract.register.RegisterPresenter
import kotlinx.android.synthetic.main.activity_register.*
import org.jetbrains.anko.toast

/**
 * @author Lc
 * @description: 2. 在activity中实现view层, 4.具体实现Presenter功能(何时触发P层)
 * @date :2021/2/23 11:16
 */
class RegisterActivity : BaseActivity(), RegisterContract.View {
    private val presenter = RegisterPresenter(this)

    override fun init() {
        super.init()
        register.setOnClickListener { register() }
        confirmPassword.setOnEditorActionListener { _, _, _ ->
            register()
            true
        }
    }

    private fun register() {
        hideSoftKeyboard()
        val userNameString = userName.text.trim().toString()
        val passwordString = password.text.trim().toString()
        val confirmPasswordString = confirmPassword.text.trim().toString()

        presenter.register(userNameString, passwordString, confirmPasswordString)
    }

    override fun getLayoutResId(): Int = R.layout.activity_register
    override fun onUserNameError() {
        userName.error = getString(R.string.user_name_error)
    }

    override fun onPasswordError() {
        password.error = getString(R.string.password_error)
    }

    override fun onConfirmPasswordError() {
        confirmPassword.error = getString(R.string.confirm_password_error)
    }

    override fun onStartRegister() {
        showProgress(getString(R.string.registering))
    }

    override fun onRegisterSuccess() {
        dismissProgress()
        finish()
    }

    override fun onRegisterFailed(message: String?) {
        dismissProgress()
        toast(R.string.register_failed)
    }

    override fun onUserExist() {
        dismissProgress()
        toast(R.string.user_already_exist)
    }
}